package servlet;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.SessionCookieConfig;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import java.io.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
public class GetProjectServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private String id="";
    private String name="";
    private String code="";
    private String description="";
    private String id_type="";
    private String pm="";
    private String is_template="";
    private String status="";
    private String plan_start_date="";
    private String plan_end_date="";
    private String real_start_date="";
    private String real_end_date="";
    private String created="";
    private String creator="";
    private String updated="";
    private String updator="";
    private String deleted="";
    private String deletion_time="";
    private String id_company="";
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/xml");
        PrintWriter out = response.getWriter();
        id=request.getParameter("id");
        int ID=Integer.parseInt(id);
        try
        {
        	Connection con;
            Statement st;
            ResultSet rs;
            Class.forName("org.postgresql.Driver");
            con=DriverManager.getConnection("jdbc:postgresql://localhost/buzzcomn","postgres","root");
            st = con.createStatement();
            String sql="select * from project where id='"+ID+"'";
            rs = st.executeQuery(sql);
            if(rs.next())
            {
            	name=rs.getString("name");
                code=rs.getString("code");
                description=rs.getString("description");
                id_type=String.valueOf(rs.getInt("id_type"));
                pm=String.valueOf(rs.getInt("pm"));
                is_template=String.valueOf(rs.getBoolean("is_template"));
                status=String.valueOf(rs.getInt("status"));
                plan_start_date=rs.getString("plan_start_date");
                plan_end_date=rs.getString("plan_end_date");
                real_start_date=rs.getString("real_start_date");
                real_end_date=rs.getString("real_end_date");
                created=rs.getString("created");
                creator=String.valueOf(rs.getInt("creator"));
                updated=rs.getString("updated");
                updator=String.valueOf(rs.getInt("updator"));
                deleted=String.valueOf(rs.getBoolean("deleted"));
                deletion_time=rs.getString("deletion_time");
                id_company=String.valueOf(rs.getInt("id_company"));      
            }
            else
            {
            	id="none";
            }
            st.close();
            con.close();
            String resultXML=createXML(id,name,code,description,id_type,pm,is_template,status,plan_start_date,plan_end_date,real_start_date,real_end_date,created,creator,updated,updator,deleted,deletion_time,id_company);
            out.println(resultXML);
               
        }
        catch(Exception e){}
    }
    protected String createXML(String _id, String _name, String _code, String _description, String _id_type, String _pm, String _is_template, String _status, String _plan_start_date, String _plan_end_date, String _real_start_date, String _real_end_date,String _created, String _creator,String _updated, String _updator, String _deleted,String _deletion_time, String _id_company )
    {
    	try
    	{
             
             DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
             DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
             Document doc = docBuilder.newDocument();

            
             Element project = doc.createElement("project");
             doc.appendChild(project);
             Element e_id = doc.createElement("id");
             project.appendChild(e_id);
             Text t_id = doc.createTextNode(_id);
             e_id.appendChild(t_id);
             Element e_name = doc.createElement("name");
             project.appendChild(e_name);
             Text t_name = doc.createTextNode(_name);
             e_name.appendChild(t_name);
             Element e_code = doc.createElement("code");
             project.appendChild(e_code);
             Text t_code = doc.createTextNode(_code);
             e_code.appendChild(t_code);
             Element e_description = doc.createElement("description");
             project.appendChild(e_description);
             Text t_description = doc.createTextNode(_description);
             e_description.appendChild(t_description);
             Element e_id_type = doc.createElement("id_type");
             project.appendChild(e_id_type);
             Text t_id_type = doc.createTextNode(_id_type);
             e_id_type.appendChild(t_id_type);
             Element e_pm = doc.createElement("pm");
             project.appendChild(e_pm);
             Text t_pm = doc.createTextNode(_pm);
             e_pm.appendChild(t_pm);
             Element e_is_template = doc.createElement("is_template");
             project.appendChild(e_is_template);
             Text t_is_template = doc.createTextNode(_is_template);
             e_is_template.appendChild(t_is_template);
             Element e_status = doc.createElement("status");
             project.appendChild(e_status);
             Text t_status = doc.createTextNode(_status);
             e_status.appendChild(t_status);
             Element e_plan_start_date = doc.createElement("plan_start_date");
             project.appendChild(e_plan_start_date);
             Text t_plan_start_date = doc.createTextNode(_plan_start_date);
             e_plan_start_date.appendChild(t_plan_start_date);
             Element e_plan_end_date = doc.createElement("plan_end_date");
             project.appendChild(e_plan_end_date);
             Text t_plan_end_date = doc.createTextNode(_plan_end_date);
             e_plan_end_date.appendChild(t_plan_end_date);
             Element e_real_start_date = doc.createElement("real_start_date");
             project.appendChild(e_real_start_date);
             Text t_real_start_date = doc.createTextNode(_real_start_date);
             e_real_start_date.appendChild(t_real_start_date);
             Element e_real_end_date = doc.createElement("real_end_date");
             project.appendChild(e_real_end_date);
             Text t_real_end_date = doc.createTextNode(_real_end_date);
             e_real_end_date.appendChild(t_real_end_date);
             Element e_created = doc.createElement("created");
             project.appendChild(e_created);
             Text t_created = doc.createTextNode(_created);
             e_created.appendChild(t_created);
             Element e_creator = doc.createElement("creator");
             project.appendChild(e_creator);
             Text t_creator = doc.createTextNode(_creator);
             e_creator.appendChild(t_creator);
             Element e_updated = doc.createElement("updated");
             project.appendChild(e_updated);
             Text t_updated = doc.createTextNode(_updated);
             e_updated.appendChild(t_updated);
             Element e_updator = doc.createElement("updator");
             project.appendChild(e_updator);
             Text t_updator = doc.createTextNode(_updator);
             e_updator.appendChild(t_updator);
             Element e_deleted = doc.createElement("deleted");
             project.appendChild(e_deleted);
             Text t_deleted = doc.createTextNode(_deleted);
             e_deleted.appendChild(t_deleted);
             Element e_deletion_time = doc.createElement("deletion_time");
             project.appendChild(e_deletion_time);
             Text t_deletion_time = doc.createTextNode(_deletion_time);
             e_deletion_time.appendChild(t_deletion_time);
             Element e_id_company = doc.createElement("id_company");
             project.appendChild(e_id_company);
             Text t_id_company = doc.createTextNode(_id_company);
             e_id_company.appendChild(t_id_company);

             /////////////////
             //Output the XML

             //set up a transformer
             TransformerFactory transfac = TransformerFactory.newInstance();
             Transformer trans = transfac.newTransformer();
             trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
             trans.setOutputProperty(OutputKeys.INDENT, "yes");

             //create string from xml tree
             StringWriter sw = new StringWriter();
             StreamResult result = new StreamResult(sw);
             DOMSource source = new DOMSource(doc);
             trans.transform(source, result);
             String xmlString = sw.toString();

             
             return xmlString;

         } 
    	 catch (Exception e) 
         {
             return "error";
         }
	}

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }


    @Override
    public String getServletInfo() {
        return "Short description";
    }


}
